package com.salon.modules.app.controller;

import com.salon.common.http.response.ObjectResult;
import com.salon.common.http.response.ResultCode;
import com.salon.modules.app.annotation.Login;
import com.salon.modules.app.form.EditPasswordForm;
import com.salon.modules.app.form.LoginForm;
import com.salon.modules.app.utils.JwtUtils;
import com.salon.modules.makeup.entity.UserEntity;
import com.salon.modules.makeup.service.UserService;
import com.salon.modules.oss.entity.LocalFile;
import com.salon.modules.oss.service.LocalFileService;
import com.salon.modules.oss.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 */
@RestController
@RequestMapping("/app")
@Api(tags = "登录接口")
public class LoginController {

    @Resource
    private UserService userService;

    @Resource
    private JwtUtils jwtUtils;

    @Resource
    private LocalFileService localFileService;

    @PostMapping("login")
    @ApiOperation("登录")
    public ObjectResult<Map<String, Object>> login(@RequestBody LoginForm form) {
        UserEntity user = userService.queryByMobile(form.getMobile());
        if (ObjectUtils.isEmpty(user)) {
            return ObjectResult.failed(ResultCode.FAIL, "手机号或密码错误");
        }
        if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))) {
            return ObjectResult.failed(ResultCode.FAIL, "手机号或密码错误");
        }
        //返回屏蔽密码字段
        user.setPassword(null);
        String token = jwtUtils.getAppToken(user.getUserId());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return ObjectResult.success(map);
    }

    @Login
    @PostMapping("saveUserInfo")
    @ApiOperation("保存个人资料")
    public ObjectResult<String> saveUserInfo(UserEntity user, @RequestAttribute("userId") Long userId) {
        if (ObjectUtils.isEmpty(userId)) {
            return ObjectResult.failed(ResultCode.FAIL, "参数错误");
        }
        user.setUserId(userId);
        userService.updateById(user);
        return ObjectResult.success();
    }

    @Login
    @GetMapping("getUserInfo")
    @ApiOperation("获取个人资料")
    public ObjectResult<UserEntity> getUserInfo(@RequestAttribute("userId") Long userId) {
        if (ObjectUtils.isEmpty(userId)) {
            return ObjectResult.failed(ResultCode.FAIL, "参数错误");
        }
        return ObjectResult.success(userService.getById(userId));
    }

    @Login
    @PostMapping("editPassword")
    @ApiOperation("修改密码")
    public ObjectResult<String> editPassword(@RequestBody EditPasswordForm form, @RequestAttribute("userId") Long userId) {
        UserEntity user = userService.getById(userId);
        if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOldPassword()))) {
            return ObjectResult.failed(ResultCode.FAIL, "旧的错误密码");
        }
        user.setPassword(DigestUtils.sha256Hex(form.getNewPassword()));
        userService.updateById(user);
        return ObjectResult.success();
    }

    @Login
    @PostMapping("saveUserAvatar")
    @ApiOperation("修改头像")
    public ObjectResult<?> saveUserAvatar(MultipartFile file, @RequestAttribute("userId") Long userId) {
        if (file.isEmpty()) {
            return ObjectResult.failed(ResultCode.FAIL, "头像上传失败！");
        }
        String fileName = FileUtil.getFileNameNoEx(file.getOriginalFilename());
        LocalFile localFileEntity = localFileService.create(fileName, file);
        UserEntity avatarUser = new UserEntity();
        avatarUser.setUserId(userId);
        avatarUser.setHeadPic(localFileEntity.getType() + "/" + localFileEntity.getRealName());
        if (userService.updateById(avatarUser)) {
            return ObjectResult.success(avatarUser.getHeadPic());
        }
        return ObjectResult.failed(ResultCode.FAIL, "头像上传失败！");
    }

}
